package cn.cqs.redisitem.controller;

import cn.cqs.redisitem.dto.BaseDTO;
import cn.cqs.redisitem.entity.PcUser;
import cn.cqs.redisitem.service.IPcUserService;
import cn.cqs.redisitem.utils.redis.RedisUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Nonnull;
import java.util.List;

/**
 * @discription :
 * @author: cqs
 * @date: 2020-09-15 8:11
 */
@RestController
public class PcUserController {
    @Autowired
    IPcUserService pcUserSerivce;
    @Autowired
    RedisUtil redisutil;
/**
 * 功能描述 :用户注册
 * @author cqs
 * @date 2020/9/26
 * @param sysUser:
 * @return cn.cqs.redisitem.dto.BaseDTO
 */

    @PostMapping("/pc/sysregister")
    public BaseDTO register(@RequestBody @Nonnull PcUser sysUser) {
        BaseDTO baseDTO;
        try {
            pcUserSerivce.addUser(sysUser);
            baseDTO = new BaseDTO(HttpStatus.OK.value(), HttpStatus.OK.name(), null);
        } catch (Exception e) {
            e.printStackTrace();
            baseDTO = new BaseDTO(HttpStatus.FAILED_DEPENDENCY.value(), HttpStatus.FAILED_DEPENDENCY.name(), null);
        }
        return baseDTO;

    }

    @PostMapping("/pc/syslogin")
    public BaseDTO login(@RequestBody  @Nonnull PcUser sysUser ){
        BaseDTO baseDTO = null;
        try {
            String username=sysUser.getUsername();
            if(StringUtils.isNotBlank(username)){
                pcUserSerivce.findByName(sysUser.getUsername());
                baseDTO = new BaseDTO(HttpStatus.OK.value(), HttpStatus.OK.name(), null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseDTO = new BaseDTO(HttpStatus.FAILED_DEPENDENCY.value(), HttpStatus.FAILED_DEPENDENCY.name(), null);
        }
        return baseDTO;

    }

    @DeleteMapping("/pc/deleteUser/{id}")
    public BaseDTO deleteUser(@PathVariable("id") @Nonnull String id){
        BaseDTO baseDTO;
        try {
            pcUserSerivce.deletUser(id);
            baseDTO = new BaseDTO(HttpStatus.OK.value(), HttpStatus.OK.name(), null);
        } catch (Exception e) {
            e.printStackTrace();
            baseDTO = new BaseDTO(HttpStatus.FAILED_DEPENDENCY.value(), HttpStatus.FAILED_DEPENDENCY.name(), null);
        }
        return baseDTO;
    }
    @PutMapping("/pc/updateUser/{id}")
    public BaseDTO updateUser(@PathVariable("id") @Nonnull String id,@RequestBody PcUser sysUser){
        BaseDTO baseDTO;
        try {
            sysUser.setId(Integer.valueOf(id));
            pcUserSerivce.updateUser(sysUser);
            baseDTO = new BaseDTO(HttpStatus.OK.value(), HttpStatus.OK.name(), null);
        } catch (Exception e) {
            e.printStackTrace();
            baseDTO = new BaseDTO(HttpStatus.FAILED_DEPENDENCY.value(), HttpStatus.FAILED_DEPENDENCY.name(), null);
        }
        return baseDTO;
    }



    @GetMapping("/pc/findAllUser")
    public BaseDTO findAllUser() {
        BaseDTO baseDTO;
        try {
            List<PcUser> sysUserList = pcUserSerivce.findAll();
            if(ObjectUtils.isNotEmpty(sysUserList)){
                baseDTO = new BaseDTO(HttpStatus.OK.value(), HttpStatus.OK.name(), sysUserList);
            }else {
                baseDTO = new BaseDTO(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.name(), null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseDTO = new BaseDTO(HttpStatus.FAILED_DEPENDENCY.value(), HttpStatus.FAILED_DEPENDENCY.name(), null);
        }
        return baseDTO;
    }

    @GetMapping("/pc/findByIdUSer/{id}")
    public BaseDTO findByIdUser(@PathVariable("id") @Nonnull String id) {
        BaseDTO baseDTO;
        try {
            PcUser sysUser = pcUserSerivce.findById(id);
           if(ObjectUtils.isNotEmpty(sysUser)){
               baseDTO = new BaseDTO(HttpStatus.OK.value(), HttpStatus.OK.name(), sysUser);
           }else{
               baseDTO = new BaseDTO(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.name(), null);
           }
        } catch (Exception e) {
            e.printStackTrace();
            baseDTO = new BaseDTO(HttpStatus.FAILED_DEPENDENCY.value(), HttpStatus.FAILED_DEPENDENCY.name(), null);
        }
        return baseDTO;
    }
    @GetMapping("/pc/findByUsername/{username}")
    public BaseDTO findByUserName(@PathVariable("username")String username) {
        BaseDTO baseDTO;
        try {
            PcUser sysUser = pcUserSerivce.findByName(username);
            if(ObjectUtils.isNotEmpty(sysUser)){
                baseDTO = new BaseDTO(HttpStatus.OK.value(), HttpStatus.OK.name(), sysUser);
            }else {
                baseDTO = new BaseDTO(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.name(), null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseDTO = new BaseDTO(HttpStatus.FAILED_DEPENDENCY.value(), HttpStatus.FAILED_DEPENDENCY.name(), null);
        }
        return baseDTO;

    }

}
